package com.loadmore.common.net

import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.Callback
import com.lzy.okgo.callback.StringCallback
import java.io.File
import java.util.*

/**
 * @author sunleilei
 * @date 2018/7/12
 * @time 8:52
 */
object Request {

    fun <T> get(url: String, params: HashMap<String, Any>?, callback: Callback<T>) {
        var urlParams = "?"
        if(params != null) {
            var iterator = params.entries.iterator()
            while (iterator.hasNext()) {
                val entry = iterator.next()
                urlParams += "&${entry.key}=${entry.value}"
            }
        }
        val request = OkGo.get<T>(url+urlParams)
        request.execute(callback)
    }

    fun <T> get(url: String, callback: Callback<T>) {
        val request = OkGo.get<T>(url)
        request.execute(callback)
    }
    fun get(url: String,callback:StringCallback) {

        val request = OkGo.get<String>(url).execute(callback)
    }
    fun <T> post(url: String, params: HashMap<String, Any>, callback: Callback<T>) {
        val request = OkGo.post<T>(url)
        var iterator = params.entries.iterator()
        while (iterator.hasNext()) {
            val entry = iterator.next()
            val value = entry.value

            if (value is String) {
                request.params(entry.key, value)
            }else if (value is File) {
                request.params(entry.key, value)
            }else if (value is Int) {
                request.params(entry.key, value)
            }
        }
        request.execute(callback)
    }


    fun <T> delete(url: String, params: HashMap<String, Any>, callback: Callback<T>) {
        val request = OkGo.post<T>(url)
        var iterator = params.entries.iterator()
        while (iterator.hasNext()) {
            val entry = iterator.next()
            val value = entry.value

            if (value is String) {
                request.params(entry.key, value)
            }else if (value is File) {
                request.params(entry.key, value)
            }else if (value is Int) {
                request.params(entry.key, value)
            }
        }
        request.execute(callback)
    }
//
//    fun post(url: String, params: HashMap<String, String>, callback: StringCallback) {
//        val request = OkGo.post<String>(url)
//        var iterator = params.entries.iterator()
//        while (iterator.hasNext()) {
//            val entry = iterator.next()
//            request.params(entry.key, entry.value)
//        }
//        request.execute(callback)
//    }


}
